/* Endless Code Copyright (C) 2009 Jingul,Kim
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 */
package ec.gfx.ui;

import java.nio.CharBuffer;

import ec.Result;
import ec.gfx.GL;
import ec.gfx.Scene;
import ec.gfx.font.Font;
import ec.gfx.font.Glyph;

/**
 * @since 2010
 * @version 1.0
 * @author <a href="mailto:jingul.kim@gmail.com">Jingul,Kim</a>
 */
public class Text extends Part {
	private CharBuffer cb;
	private Font font;

	public Text(Font font, String text) {
		this.font = font;

		setChars(text);
	}

	@Override
	public Result now(Scene scene) {
		final char[] ary = cb.array();
		
		GL.pushAttrib(GL.GL_LIST_BIT | GL.GL_CURRENT_BIT | GL.GL_ENABLE_BIT
				| GL.GL_TRANSFORM_BIT);

		GL.enable(GL.GL_TEXTURE_2D);
		GL.enable(GL.GL_BLEND);
		GL.blendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA);

		GL.color3f(1.0f, 1.0f, 1.0f);

		for (char c : ary) {
			Glyph g = font.getGlyph(c);
			g.render();
		}

		GL.popAttrib();

		return Result.OK;
	}
	
	public void setChars(String text) {
		cb = CharBuffer.wrap(text.toCharArray());
	}
}
